package com.ericsson.rm.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.ericsson.rm.model.EmpDetail;
import com.ericsson.rm.model.ProjectDetail;
import com.ericsson.rm.response.ProjectDetailsResponse;

@Repository
public class ResourceAllocateDaoImpl implements ResourceAllocateDao{

	@PersistenceContext
	EntityManager entityManager;
	
	@Override
	public EmpDetail checkEmployeeIfPresentInDB(String empID)
	{ 
		try {
			Query q = entityManager.createNamedQuery("EmpDetail.searchEmployee",EmpDetail.class)
					.setParameter("empID", empID);
				return (EmpDetail) q.getSingleResult();
		} catch (Exception e) {
			return null;
		}
		
	}
	
	
	@Override
	@SuppressWarnings("unchecked")
	public List<String> getProjectNameFromDB(String projName)
	{
		System.out.println("in getProjectNameFromDB method----------------------------------------"+projName);
		try
		{
			Query q = entityManager.createNamedQuery("ProjectDetail.getProjNameList",String.class)
					  .setParameter("projName", "%"+projName+"%");
			System.out.println(q.getResultList());
			return q.getResultList();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	
	
	
	
	@Override
	@SuppressWarnings("unchecked")
	public List<ProjectDetailsResponse> getProjectDetailFromDB(String projName)
	{
		System.out.println("in getProjectNameFromDB method----------------------------------------"+projName);
		try
		{
			Query q = entityManager.createNamedQuery("ProjectDetail.getProjDetailList")
					  .setParameter("projName", "%"+projName+"%");
			System.out.println(q.getResultList());
			return (List<ProjectDetailsResponse>)q.getResultList();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	
	
}
